///////////////////////////////////////////////////////////
// 
//  Game.cpp
//
//  Wiicussion
//  HCI 575 Class project
//  Spring 2007
// 
//  Authors:  Jeremy Bennett
//            Adam Faeth
//            Michael Oren
//
///////////////////////////////////////////////////////////

// Local includes
#include "stdafx.h"
#include "Game.h"
#include "MyImage.h"
#include "MyContext.h"

// SDL includes
#include <sdl.h>
#include <SDL_opengl.h>

// System includes


// constructor/destructor
Game::Game(float fX, float fY, float fW, float fH)
: _bActive(false),
_fRenderPos(),
_fRenderSize()
{
    _fRenderPos[0] = fX;
    _fRenderPos[1] = fY;
    _fRenderSize[0] = fW;
    _fRenderSize[1] = fH;
}

Game::~Game()
{
    // do nothing
}


// Activates the game
bool 
Game::activate() 
{
    _bActive = true;
    
    return _bActive;
}


// Deactivates the game
bool 
Game::deactivate() 
{
    _bActive = false;
    
    return true;
}


// Handle game events
// Since this is the base game, only look for escape and quit
bool 
Game::handleEvents()
{
    bool bRes = false;
    
    if(_bActive) {
        SDL_Event   event;
        
        // Grab events from the queue
        while ( SDL_PollEvent(&event) ) {
            switch (event.type) {
                case SDL_KEYDOWN:
                    if ( event.key.keysym.sym != SDLK_ESCAPE ) {
                        break;  // Allow escape key to be used to quit
                    }
                case SDL_QUIT:
                    _bActive = false;
                    break;
                default:
                    break;
            }
        }
        bRes = true;
    }
    
    return bRes;
}

bool 
Game::preFrame()
{
    // do nothing
    return true;
}

bool 
Game::postFrame()
{
    // do nothing
    return true;
}

bool
Game::renderGame( MyContext *pContext )
{
    glPushMatrix();
    glLoadIdentity();
    
    glTranslatef(_fRenderPos[0],_fRenderPos[1],0.0f);
    
    float sf_bgColor[]  = { 0.0f, 0.0f, 1.0f };
    
    MyImage *pImage = (MyImage*)pContext->texture(PLAYER_BACKGROUND_TEX);
    
    pImage->bindTexture();
    pImage->enableTexture();
    
    // Render Menu
    glColor3fv(sf_bgColor);
    
    glBegin(GL_TRIANGLE_STRIP);
    // Vertex 0
    glTexCoord2f(0.0f,0.0f);
    glVertex3f(0.0f,0.0f,0.0f);
    // Vertex 1
    glTexCoord2f(0.0f,1.0f);
    glVertex3f(0.0f,_fRenderSize[1],0.0f);
    // Vertex 2
    glTexCoord2f(1.0f,0.0f);
    glVertex3f(_fRenderSize[0],0.0f,0.0f);
    // Vertex 3
    glTexCoord2f(1.0f,1.0f);
    glVertex3f(_fRenderSize[0],_fRenderSize[1],0.0f);
    glEnd();
    
    pImage->disableTexture();
    
    glPopMatrix();
    
    
    
    return true;
}    

